package com.lb.shopguide.config;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by zsd on 2016/12/13.
 */

public class BaseSpConfig {
    protected Context mContext;
    /**
     * 指定文件名称的名称的SharedPreferences对象
     */
    private SharedPreferences sp;
    /**
     * 指定文件名称的名称的编辑器对象
     */
    private SharedPreferences.Editor edit;

    public void initBaseConfig(Context ctx) {
        mContext = ctx;
    }

    private void init(String spFileName) {
        if (null == sp || null == edit) {
            sp = mContext.getSharedPreferences(spFileName, Context.MODE_MULTI_PROCESS);
            edit = sp.edit();
        }
    }

    public String getString(String spFileName, String key, final String defaultValue) {
        init(spFileName);
        return sp.getString(key, defaultValue);
    }

    public void setString(String spFileName, final String key, final String value) {
        init(spFileName);
        edit.putString(key, value);
    }

    public boolean getBoolean(String spFileName, final String key, final boolean defaultValue) {
        init(spFileName);
        return sp.getBoolean(key, defaultValue);
    }

    public void setBoolean(String spFileName, final String key, final boolean value) {
        init(spFileName);
        edit.putBoolean(key, value);
    }

    public void setInt(String spFileName, final String key, final int value) {
        init(spFileName);
        edit.putInt(key, value);
    }

    public int getInt(String spFileName, final String key, final int defaultValue) {
        init(spFileName);
        return sp.getInt(key, defaultValue);
    }

    public void setFloat(String spFileName, final String key, final float value) {
        init(spFileName);
        edit.putFloat(key, value);
    }

    public float getFloat(String spFileName, final String key, final float defaultValue) {
        init(spFileName);
        return sp.getFloat(key, defaultValue);
    }

    public void setLong(String spFileName, final String key, final long value) {
        init(spFileName);
        edit.putLong(key, value);
    }

    public long getLong(String spFileName, final String key, final long defaultValue) {
        init(spFileName);
        return sp.getLong(key, defaultValue);
    }

    /**
     * @param context
     * @param spFileName
     * @param key
     * @return
     * @desc 是否有关键字
     */
    public boolean hasKey(Context context, String spFileName, final String key) {
        init(spFileName);
        return sp.contains(key);
    }

    /**
     * @desc 提交
     * apply支持异步提交，commit是同步提交
     * apply没有返回值，commit有返回值，true为成功，false失败
     */
    public void commit() {
        if (null != edit) {
            edit.apply();
        }
    }

    /**
     * @param context
     * @desc 清除SharedPreferences所有数据
     */
    public void clearPreference(Context context) {
        if (null != sp) {
            final SharedPreferences.Editor editor = sp.edit();
            editor.clear();
            editor.apply();
        }
    }

    /**
     * @desc 保存信息到SharedPreferences
     */
    protected void save2Sp() {
        commit();
    }

    /**
     * @param isSave
     * @desc 保存信息到SharedPreferences <br/>
     * 注：此函数提供给一次只保存一个配置的情况，请不要滥用。
     */
    public void save2Sp(boolean isSave) {
        if (isSave) {
            save2Sp();
        }
    }
}
